import { CompoundColor } from "@/module/labMap"
import { CreateBox } from "@/visual/base"

/* 房间通用运行主程序 */
export default ()=>{

    if (!Memory.ControlPlan) Memory.ControlPlan = {}
    for (var roomName in Memory.ControlPlan)
    {
        if (!Game.rooms[roomName]) delete Memory.ControlPlan[roomName]
        if (!global.CreepCpu) global.CreepCpu = {}
        var startRoomCpu = Game.cpu.getUsed()
        var thisRoom = Game.rooms[roomName]
        if (!thisRoom || !thisRoom.controller.my) continue
        /* 建筑数据更新 */
        thisRoom.UpdateStructureId()
        /* 全局建筑变量更新 */
        thisRoom.GlobalStructure()
        /* 中心点检查 */
        if (!thisRoom.memory.center_x || !thisRoom.memory.center_y)
            continue
        /* 角色挂载 */
        thisRoom.MountSpawnData()

        /* 角色更新 */
        thisRoom.ModifySpawnData()

        /* 任务控制 */
        thisRoom.TaskManager()

        /* 房间建筑执行任务 */
        var AllStructures =  thisRoom.getTypeStructure(['link','terminal','factory']) as StructureLink[]
        for (var stru of AllStructures)
        {
            if (stru.ManageMisson)
                stru.ManageMisson()
        }
        /* 防御塔凑合函数 */
        // var a = Game.cpu.getUsed()
        thisRoom.TowerWork()
        // var b = Game.cpu.getUsed()
        // console.log(thisRoom.name,'  Tower cpu:',b-a)
        /* 房间状态识别 */
        thisRoom.RoomState()

        /* 自动布局 */
        thisRoom.AutoArrange()

        /* 爬虫数量手动控制 */
        thisRoom.FinalSpawnData()
        
        /* 爬虫孵化 */
        thisRoom.CreepSpawn()

        var endRoomCpu = Game.cpu.getUsed()
        /* visual 房间基本信息可视化 */
        CreateBox(roomName,0,0,7,10)
        thisRoom.visual.text(`全局CPU:${(global.UsedCpu?global.UsedCpu:0).toFixed(2)}`,0,1,{color: 'black', font:0.7,align:'left'})
        thisRoom.visual.text(`全局平均CPU:${(global.AveCpu?global.AveCpu:0).toFixed(2)}`,0,2,{color: 'black', font:0.7,align:'left'})
        thisRoom.visual.text(`房间框架CPU:${(endRoomCpu-startRoomCpu).toFixed(2)}`,0,3,{color: 'black', font:0.7,align:'left'})
        thisRoom.visual.text(`房间爬虫CPU:${(global.CreepCpu[roomName]?global.CreepCpu[roomName]:0).toFixed(2)}`,0,4,{color: 'black', font:0.7,align:'left'})
        thisRoom.visual.text(`房间状态:${(thisRoom.memory.roomSTATE)}`,0,5,{color: thisRoom.memory.roomSTATE == 'peace'?'blue':'red', font:0.7,align:'left'})
        let processController = thisRoom.controller.level >= 8?100:((thisRoom.controller.progress/thisRoom.controller.progressTotal)*100).toFixed(4)
        thisRoom.visual.text(`控制器进度:${processController}%`,0,6,{color: 'black', font:0.7,align:'left'})
        /* 目前存在任务数 */
        var MissonNum = 0
        for (var range in thisRoom.memory.Misson)
            MissonNum += Object.keys(thisRoom.memory.Misson[range]).length
        thisRoom.visual.text(`房间任务数:${MissonNum}`,0,7,{color: MissonNum>0?'blue':'black', font:0.7,align:'left'})
        thisRoom.visual.text(`cpu池:${Game.cpu.bucket}`,0,8,{color: 'black', font:0.7,align:'left'})
        if (Object.keys(thisRoom.memory.RoomLabBind).length > 0)
        {
            for (var i in thisRoom.memory.RoomLabBind)
            {
                let lab_ = Game.getObjectById(i) as StructureLab
                if (lab_) thisRoom.visual.text(`${thisRoom.memory.RoomLabBind[i].rType}`,lab_.pos.x,lab_.pos.y,{color:CompoundColor[thisRoom.memory.RoomLabBind[i].rType],font:0.3,align:'center',strokeWidth:0.2})
            }
        }

    }
}